<?php

/**
 * Class AddressController
 * 处理个人模块收货地址相关业务
 */
class AddressController extends \BaseController
{
    /**
     * 获取收货地址列表清单
     * 返回json格式:
     * ```
     * {
     *   "addressList":
     *   [
     *     {
     *       "id":
     *       "name":
     *
     *       "district": {
     *          province: {
     *            id:,
     *            name:
     *          },
     *          city: {
     *            id:,
     *            name:
     *          },
     *          region: {
     *            id:,
     *            name:
     *          }
     *        },
     *
     *       "location":
     *       "phone":
     *       "postcode":
     *       "default": boolean
     *      }
     *   ]
     * }
     * ```
     */
    public function index()
    {
        return Response::json([
            'addressList' => Address::getAddressList(Auth::user()['id'])
        ]);
    }

    /**
     * 新增收货地址
     * 请求json格式:
     * ```
     * {
     *   "name":
     *   "regionId":
     *   "location":
     *   "phone":
     *   "postcode":
     *   "default": boolean
     * }
     * ```
     */
    public function store()
    {
        $resq = Input::all();
        $retains = ['name', 'regionId', 'location', 'phone', 'postcode', 'default'];
        $filterData = $this->postFilter($resq, $retains);

        $validator = $this->getValidatorCheckWith($filterData);

        if ($validator->fails()) {
            return parent::failResponse($validator->messages());
        } else {
            $filterData['user_id'] = Auth::user()['id'];
            $filterData['third_level'] = $filterData['regionId'];
            $filterData['created_at'] = date('Y-m-d');
            $filterData['updated_at'] = date('Y-m-d');
            unset ($filterData['regionId']);
            $add = Address::create($filterData);
            Address::refreshDefault($add['id'], $filterData['default']);
            return Response::json([$add['id']]);
        }
    }

    /**
     * 获取某一收货地址详细信息
     * 返回json格式:
     * ```
     * {
     *   "id":
     *   "name":
     *   "district": {
     *      province: {
     *        id:,
     *        name:
     *      },
     *      city: {
     *        id:,
     *        name
     *      },
     *      region: {
     *        id:,
     *        name
     *      }
     *    },
     *
     *   "location":
     *   "phone":
     *   "postcode":
     *   "default": boolean
     * }
     * ```
     */
    public function show($id)
    {
        $baseInfo = Address::getBaseInfo(Auth::user()['id'], false, $id);
        $district = Address::getDistrictByThirdId($baseInfo['third_level']);
        $baseInfo['district'] = $district;
        return Response::json($baseInfo);
    }

    /**
     * 更新某一收货地址
     * 请求json格式:
     * ```
     * {
     *   "name":,
     *   "regionId":
     *   "location":
     *   "phone":
     *   "postcode":
     *   "default": boolean
     * }
     * ```
     */
    public function update($id)
    {
        $resq = Input::all();
        $retains = ['name', 'regionId', 'location', 'phone', 'postcode', 'default'];
        $filterData = $this->postFilter($resq, $retains);

        $validator = $this->getValidatorCheckWith($filterData);

        if ($validator->fails()) {
            return parent::failResponse($validator->messages());
        } else {
            $filterData['third_level'] = $filterData['regionId'];
            $filterData['updated_at'] = date('Y-m-d');
            unset ($filterData['regionId']);
            Address::findOrFail($id)->update($filterData);
            Address::refreshDefault($id, $filterData['default']);
            return parent::successResponse();
        }
    }

    /**
     * 删除某一收货地址
     * 返回json格式:
     * ```
     * {
     *   "status": "success"
     *   "message": "操作成功"
     * }
     * ```
     */
    public function destroy($id)
    {
        try {
            Address::findOrFail($id)->delete();
        } catch (Exception $e) {
            return parent::failResponse('内部服务错误.');
        }

        return parent::successResponse();
    }


    protected function getValidatorCheckWith($filteredData)
    {
        return Validator::make($filteredData, $this->getRules());
    }

    protected function getRules()
    {
        return [
            'name' => 'required|max:50',
            'location' => 'required|max:100',
            'phone' => 'required|max:11',
            'postcode' => 'required|max:6',
        ];
    }

    protected function getValidateMsg()
    {
        return [
            'required' => '该选项不能为空',
            'max:50' => '收货人名超过长度限制',
            'integer' => '数据格式错误',
            'max:100' => '收货地址超过长度限制',
            'max:11' => '联系号码超过长度限制',
            'max:6' => '邮编超过长度限制'
        ];
    }
}
